package com.qupai.controller.admin;

import com.qupai.dto.PrizesDTO;
import com.qupai.dto.PrizesPageQueryDTO;
import com.qupai.entity.Prizes;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.service.PrizesService;
import com.qupai.vo.PrizesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/prizes")
@Tag(name = "志愿-奖品管理相关接口")
@Slf4j
public class PrizesController {

    @Resource
    private PrizesService prizesService;
    @Resource
    private RedisTemplate redisTemplate;


    @PostMapping
    @Operation(summary = "新增奖品")
    public Result save(@RequestBody PrizesDTO prizesDTO) {
        log.info("新增奖品:{}", prizesDTO);
        // 调用service保存奖品信息
        prizesService.save(prizesDTO);

        //清理缓存数据
        String key = "prizes_" + prizesDTO.getCollegesId();
        cleanCache(key);

        return Result.success();
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询奖品")
    public Result<PageResult> page(@ParameterObject PrizesPageQueryDTO prizesPageQueryDTO){
        log.info("分页查询奖品:{}", prizesPageQueryDTO);
        PageResult pageResult=prizesService.pageQuery(prizesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 奖品的批量删除
     * @param ids
     * @return
     */

    @DeleteMapping
    @Operation(summary = "批量删除奖品")
    public Result<String>  delete(@RequestParam List<Long> ids){
        log.info("批量删除奖品:{}",ids);
        prizesService.deleteBatch(ids);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("prizes_*");
        return Result.success();
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询奖品")
    public Result<PrizesVO>getById (@PathVariable Long id){
        log.info("根据id查询奖品:{}",id);
        PrizesVO prizesVO=prizesService.getById(id);
        return Result.success(prizesVO);
    }

    @PutMapping
    @Operation(summary = "修改奖品")
    public Result<String>  update(@RequestBody PrizesDTO prizesDTO){
        log.info("修改奖品:{}",prizesDTO);
        prizesService.update(prizesDTO);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("prizes_*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启用/禁用奖品状态")
    public Result<String>  startOrStop(@PathVariable Integer status, Long id){
        log.info("启用/禁用奖品状态：{}，{}",status,id);
        //调用service
        prizesService.startOrStop(status,id);

        //将所有的奖品数据缓存数据清理掉，所有以prizes_开头的key
        cleanCache("prizes_*");
        return Result.success();
    }

    /**
     * 根据各分类id查询奖品
     */
    @GetMapping("/list")
    @Operation(summary = "根据各分类id查询奖品")
    public Result<List<PrizesVO>> list(Prizes prizes){
        List<PrizesVO> list = prizesService.selectPrizesList(prizes);
        log.info("根据各分类id查询奖品：{}",list);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}


